# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.conf import settings
from dishu.items import CategoryItem
from dishu.items import RecipeItem
import pymongo

class DishuPipeline(object):

    def __init__(self):
        # 获取setting主机名、端口号和数据库名称
        self.host = settings['MONGODB_HOST']
        self.port = settings['MONGODB_PORT']
        self.dbname = settings['MONGODB_DBNAME']
        self.dbcateColl = settings['MONGODB_CATECOLL']
        self.dbrecipeColl = settings['MONGODB_RECIPECOLL']

    def open_spider(self, spider):
        self.client = pymongo.MongoClient(self.host, self.port)
        self.db = self.client[self.dbname]
        #categoryCollection
        self.catepost = self.db[self.dbcateColl]
        self.catepost.ensure_index('categoryId', unique=True)
        #recipeCollection
        self.recipepost = self.db[self.dbrecipeColl]
        self.recipepost.ensure_index('recipeId', unique=True)

    def close_spider(self, spider):
        self.client.close()

    def process_item(self, item, spider):

        if type(item) == CategoryItem:
            postItem = dict(item)
            self.catepost.insert(postItem)
        elif type(item) == RecipeItem:
            postItem1 = dict(item)
            self.recipepost.insert(postItem1)
        return item
